package com.keepsport.keepsportapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keepsport.keepsportapi.dao.CommunityLogtDao;
import com.keepsport.keepsportapi.dao.CommunityTopicDao;
import com.keepsport.keepsportapi.entity.CommunityLogt;
import com.keepsport.keepsportapi.entity.CommunityTopic;
import com.keepsport.keepsportapi.service.intf.CommunityTopicService;
import com.keepsport.keepsportapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/***
 *         O
 *      _ooOoo_
 *     o8888888o
 *     88" . "88
 *     (| -_- |)
 *      O\ = /O
 *  ____/`---'\____
 *  ...............
 *  佛祖保佑  永无BUG
 *   Data:2022/7/1
 *   Author:King
 *   Description:
 */
//热门话题的impl
@Service
public class CommunityTopicServiceImpl implements CommunityTopicService {
    @Resource
    private CommunityTopicDao dao;
    @Resource
    private CommunityLogtDao logtDao;
    //查询所有话题按热度倒序
    @Override
    public R findDesc() {
        return R.ok(dao.find());
    }
    //点击热门话标题查询详细内容
    @Override
    public R findOne(int tid) {
        return R.ok(dao.findOne(tid));
    }
    //修改话题热度，点击热度修改加1
    @Override
    public R updateHeat(int token,int tid) {
        //验证参数
        if (tid > 0) {
            if (logtDao.find(token,tid)==null) {
                //没查看过添加查看记录
                CommunityLogt communityLogt = new CommunityLogt( token,tid);
                logtDao.insert(communityLogt);
                //查询该话题信息
                CommunityTopic communityTopic = dao.selectById(tid);
                if (communityLogt!=null){
                    //该话题热度加一
                    Integer heat = communityTopic.getHeat()+1;
                    communityTopic.setHeat(heat);
                    if (dao.updateById(communityTopic) > 0) {
                        return R.ok();
                    }
                }
            }
        }
        return R.fail("热度加1失败");
    }

}
